package org.zh.rc.vll;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.zh.entity.MethodBean;
import org.zh.entity.ServiceEntity;
import org.zh.rc.sv.ServiceEntityService;

import java.util.List;
import java.util.Map;

import static org.zh.rc.config.GlobalStatic.om;
import static org.zh.util.CommonStatic.*;

@RestController
public class RCController {
    private final ServiceEntityService ses;

    public RCController(ServiceEntityService ses) {
        this.ses = ses;
    }

    @PostMapping(rcRegistry)
    public Map<String, List<Map<String, MethodBean>>> registry(@RequestBody String JSON) throws JsonProcessingException {
        ServiceEntity se = om.readValue(JSON, ServiceEntity.class);
        ses.insertBean(se);
        return ses.getPUBServer(se);
    }


    @PostMapping(rcHeartBeat)
    public Map<String, List<Map<String,MethodBean>>> heartBeat(@RequestBody ServiceEntity se) {
        ses.updateSurvivalTime(se.getApplicationName(),se.getApplicationBaseUrl());
        return ses.getPUBServer(se);
    }

    @PostMapping("test/vue")
    public List<Map<String,Object>> getAll(){
        return ses.getAll();
    }
}
